import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts";

class MonotonicQueue {
  private queue: number[] = [];

  shift() {
    return this.queue.shift();
  }

  size() {
    return this.queue.length;
  }

  min() {
    return this.queue[this.size() - 1];
  }

  getMax() {
    return this.queue[0];
  }

  push(n: number) {
    // if(this.queue)
    while (this.queue.length && this.queue[this.queue.length - 1] < n) {
      this.queue.pop();
    }

    this.queue.push(n);
  }

  isEmpty() {
    return this.queue.length === 0;
  }
}

Deno.test("MonotonicQueue", () => {
  const queue = new MonotonicQueue();
  queue.push(1);
  queue.push(1);
  queue.push(3);
  queue.push(2);
  queue.push(2);
  queue.push(1);

  assertEquals(queue.getMax(), 3);
  assertEquals(queue.shift(), 3);
  assertEquals(queue.shift(), 2);
  assertEquals(queue.min(), 1);
  assertEquals(queue.isEmpty(), false);
});
